package com.huanghwh.demo.event.publisher.impl;

import cn.hutool.log.StaticLog;
import com.huanghwh.demo.event.entity.OrderMessage;
import com.huanghwh.demo.event.listener.OrderEvent;
import com.huanghwh.demo.event.publisher.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * @Author: huanghwh
 * @Date: 2023/03/26 下午9:50
 * @Description: 消息发布demo
 */
@Service
public class OrderServiceImpl implements IOrderService {
    
    /* 使用Spring 提供的ApplicationEventPublisher来发布自定义事件 */
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;
    
    @Override
    public void saveOrder() {
        StaticLog.info("生产者：准备生成订单...");
        // ..生成订单(略)
        String orderNum = "001";
        String orderName = "眼镜";
        // 订单消息类
        StaticLog.info("准备发布订单号：{}", orderNum);
        OrderMessage orderMessage = new OrderMessage(orderNum, orderName);
        // 发布消息 入参: 订单事件监听接口(消息传输实体类)
        applicationEventPublisher.publishEvent(new OrderEvent(orderMessage));
        StaticLog.info("发布完成：{}", orderNum);
    }
    
}
